/****************************************************
 * Description: ServiceImpl for 用户组
 * Copyright:   Copyright (c) 2013
 * Company:     beiwaionline
 * @author      bfsu
 * @version     1.0
 * @see
	HISTORY
    *  2013-08-08 bfsu Create File
**************************************************/

package com.bfsuol.bfsuolframework.security.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bfsuol.bfsuolframework.core.BfsuolConstants;
import com.bfsuol.bfsuolframework.security.dao.GroupMapper;
import com.bfsuol.bfsuolframework.security.dao.GroupRoleMapper;
import com.bfsuol.bfsuolframework.security.dao.BaseUser;
import com.bfsuol.bfsuolframework.security.dao.Group;
import com.bfsuol.bfsuolframework.security.dao.GroupRole;
import com.bfsuol.bfsuolframework.security.dao.Role;
import com.bfsuol.bfsuolframework.security.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class GroupServiceImpl extends ServiceImpl<GroupMapper,Group> implements GroupService {

    @Autowired
    private GroupMapper groupMapper;
    
    @Autowired
    private GroupRoleMapper groupRoleMapper;

    
	/* (non-Javadoc)
	 * @see GroupService#save(Group, java.lang.Long[])
	 */
	public void save(Group group, Long[] roles) {
		//保存用户组
		group.setIsSys(BfsuolConstants.GLOBAL_YESNO_NO);
		group.setIsEnabled(BfsuolConstants.GLOBAL_YESNO_YES);
		groupMapper.insert(group);
		//保存用户组的角色
		if(roles.length>0){
			for(Long id :roles){
				if(id!=0){
					GroupRole gr = new GroupRole();
					gr.setGroup(group);
					gr.setIsDefault(0);
					gr.setRole(new Role(id));
					groupRoleMapper.insert(gr);
					gr = null;
				}
			}
		}
	}

	/* (non-Javadoc)
	 * @see GroupService#deleteUsers(java.lang.Long, java.lang.Long[])
	 */
	public void deleteUsers(Long groupId, Long[] ids) {
		Group group = groupMapper.selectById(groupId);
		for(Long id:ids){
			group.removeUser(new BaseUser(id));
		}
		groupMapper.updateById(group);
	}

	/* (non-Javadoc)
	 * @see GroupService#checkGroupTitle(java.lang.String, java.lang.Long)
	 */
	public boolean checkGroupTitle(String title, Long id) {
		return groupMapper.checkGroupTitle(title,id).size()>0?true:false;
	}

	public void deleteGroup(Long groupId) {
		groupRoleMapper.deleteById(groupId);
		groupMapper.deleteById(groupId);
		
	}

	/* (non-Javadoc)
	 * @see GroupService#findAllIsEnabledGroup()
	 */
	public List<Group> findAllIsEnabledGroup() {
		return groupMapper.selectList(null);
	}

	


}